// Fill out your copyright notice in the Description page of Project Settings.


#include "WindowUMGmgrActor.h"
#include "Blueprint/UserWidget.h"
#include "Slate.h"
#include "Blueprint/UserWidget.h"
#include "Widgets/Layout/SScaleBox.h"
#include "Framework/Docking/TabManager.h"
#include "STAdditionaltViewport.h"
#include "GameFramework/Actor.h"
#include "EngineUtils.h"

// Sets default values
AWindowUMGmgrActor::AWindowUMGmgrActor():
	windowPt(nullptr),AdditionaltViewport(nullptr)
{
 	// Set this actor to call Tick() every frame.  You can turn this off to improve performance if you don't need it.
	PrimaryActorTick.bCanEverTick = false;

}




//UUserWidget* AWindowUMGmgrActor::InitWindowAndUMG(TSubclassOf<UUserWidget> umgClass
//	, FVector2D windowSize, FVector2D windowPos)
//{
//
//	CloseWindow();
//
//
//	//UMGPtr = CreateWidget<UUserWidget>(GetWorld(), umgClass);
//
//	USTAdditionaltViewport* NewWidget = NewObject<USTAdditionaltViewport>();
//
//
//	SAssignNew(windowPt, SWindow)
//		.CreateTitleBar(false)
//		.LayoutBorder(FMargin(0))
//		.SizingRule(ESizingRule::FixedSize)
//		.AutoCenter(EAutoCenter::PrimaryWorkArea)
//		.Title(FText::FromString("STtest"))
//		.IsInitiallyMaximized(false)
//		.ScreenPosition(windowPos)
//		.ClientSize(windowSize)
//		.CreateTitleBar(true)
//		.SizingRule(ESizingRule::UserSized)
//		.SupportsMaximize(true)
//		.SupportsMinimize(false);
//
//
//	//TSharedPtr<SScaleBox> scaleBox;
//	//SAssignNew(scaleBox, SScaleBox)
//	//	.Stretch(EStretch::ScaleToFit)
//	//	.StretchDirection(EStretchDirection::DownOnly);
//
//	//scaleBox->SetContent(UMGPtr->TakeWidget());
//
//	//UGameViewportClient* gameViewport = GEngine->GameViewport;
//	//TSharedPtr<SViewport>   ss = gameViewport->GetGameViewportWidget();
//	//ss->SetRenderDirectlyToWindow(false);
//
//	//TSharedRef<SOverlay> ViewportOverlayWidgetRef = SNew(SOverlay);
//	//windowPt->SetContent(ViewportOverlayWidgetRef);
//
//
//	FSlateApplication::Get().AddWindow(windowPt.ToSharedRef(), true);
//	windowPt->SetContent(NewWidget->TakeWidget());
//	
//	//ViewportOverlayWidgetRef->
//
//	windowPt->Resize(windowSize);
//	windowPt->MoveWindowTo(windowPos);
//
//	//gameViewport->SetViewportOverlayWidget(windowPt, ViewportOverlayWidgetRef);
//	//ss->SetRenderDirectlyToWindow(true);
//
//
//	return UMGPtr;
//}



bool AWindowUMGmgrActor::InitWindowAndUMG(FVector2D windowSize, FVector2D windowPos) {
	if (AdditionaltViewport)
	{
		return false;
	}
	CloseWindow();


	//UMGPtr = CreateWidget<UUserWidget>(GetWorld(), umgClass);

	AdditionaltViewport = NewObject<USTAdditionaltViewport>();
	AdditionaltViewport->AddToRoot();

	SAssignNew(windowPt, SWindow)
		.CreateTitleBar(false)
		.LayoutBorder(FMargin(0))
		.SizingRule(ESizingRule::FixedSize)
		.AutoCenter(EAutoCenter::PrimaryWorkArea)
		.Title(FText::FromString("STtest"))
		.IsInitiallyMaximized(false)
		.ScreenPosition(windowPos)
		.ClientSize(windowSize)
		.CreateTitleBar(true)
		.SizingRule(ESizingRule::UserSized)
		.SupportsMaximize(true)
		.SupportsMinimize(false);


	//TSharedPtr<SScaleBox> scaleBox;
	//SAssignNew(scaleBox, SScaleBox)
	//	.Stretch(EStretch::ScaleToFit)
	//	.StretchDirection(EStretchDirection::DownOnly);

	//scaleBox->SetContent(UMGPtr->TakeWidget());

	//UGameViewportClient* gameViewport = GEngine->GameViewport;
	//TSharedPtr<SViewport>   ss = gameViewport->GetGameViewportWidget();
	//ss->SetRenderDirectlyToWindow(false);

	//TSharedRef<SOverlay> ViewportOverlayWidgetRef = SNew(SOverlay);
	//windowPt->SetContent(ViewportOverlayWidgetRef);


	FSlateApplication::Get().AddWindow(windowPt.ToSharedRef(), true);
	windowPt->SetContent(AdditionaltViewport->TakeWidget());

	//ViewportOverlayWidgetRef->

	windowPt->Resize(windowSize);
	windowPt->MoveWindowTo(windowPos);

	return true;
}




AActor* AWindowUMGmgrActor::AddActor(TSubclassOf<AActor> actorClass, FTransform tsf) {
	if (!AdditionaltViewport)
	{
		return nullptr;
	}

	UWorld* wd = AdditionaltViewport->GetViewportWorld();
	if (wd)
	{
	
		return wd->SpawnActor<AActor>(actorClass, tsf);

	}
	else {

	return nullptr;
	}

}


void AWindowUMGmgrActor::SetViewPoint(FVector p, FRotator r) 
{
	if (!AdditionaltViewport)
	{
		return;
	}
	AdditionaltViewport->SetViewLocation(p);
	AdditionaltViewport->SetViewRotation(r);

}



void AWindowUMGmgrActor::GettViewPoint(FVector& p, FRotator& r) {
	if (!AdditionaltViewport)
	{
		return;
	}
	p = AdditionaltViewport->GetViewLocation();
	r = AdditionaltViewport->GetViewRotation();
}


TArray<AActor*>  AWindowUMGmgrActor::GetAllViewActor(TSubclassOf<AActor> actorClass)
{
	TArray<AActor*> acs;
	if (AdditionaltViewport)
	{
		UWorld* wd = AdditionaltViewport->GetViewportWorld();
		if (!wd)
		{
			return acs;
		}
		for (TActorIterator<AActor> It(wd, actorClass); It; ++It)
		{
			AActor* Actor = *It;
			if (IsValid(Actor))
			{
				acs.Add(Actor);
			}
		}
	}
	return acs;
}

bool AWindowUMGmgrActor::ResetPos(FVector2D windowPos)
{

	if (windowPt.IsValid()) 
	{
		windowPt->MoveWindowTo(windowPos);
		return true;
	}
	return false;

}


bool AWindowUMGmgrActor::ResetSize(FVector2D windowSize)
{
	if (windowPt.IsValid())
	{
		windowPt->Resize(windowSize);
		return true;
	}
	return false;
}


bool AWindowUMGmgrActor::ResetWindowMode(ESTwindowMode wm) 
{
	if (windowPt.IsValid())
	{
		windowPt->SetWindowMode(USTWindowInfo::GetWindowMode(wm));
		return true;
	}
	return false;
}



bool AWindowUMGmgrActor::GetWindowInfo(FVector2D& windowPos, FVector2D& windowSize, ESTwindowMode& wm)
{
	if (windowPt.IsValid())
	{
		windowSize = windowPt->GetViewportSize();
		windowPos = windowPt->GetPositionInScreen();
		wm = USTWindowInfo::GetStWindowMode(windowPt->GetWindowMode());
		return true;
	}
	return false;
}

bool AWindowUMGmgrActor::CloseWindow()
{
	if (AdditionaltViewport) {
		AdditionaltViewport->RemoveFromRoot();
		AdditionaltViewport = nullptr;
	}

	if (windowPt.IsValid()) {

		FSlateApplication::Get().RequestDestroyWindow(windowPt.ToSharedRef());
		//windowPt->RequestDestroyWindow();
		windowPt.Reset();
		return true;
	}
	return false;
}
// Called when the game starts or when spawned
void AWindowUMGmgrActor::BeginPlay()
{
	Super::BeginPlay();
	
}

void AWindowUMGmgrActor::EndPlay(const EEndPlayReason::Type EndPlayReason) 
{
	CloseWindow();
	Super::EndPlay(EndPlayReason);
}

// Called every frame
void AWindowUMGmgrActor::Tick(float DeltaTime)
{
	Super::Tick(DeltaTime);

}